package com.tanhua.server.controller;

import com.tanhua.domain.vo.PageResult;
import com.tanhua.server.serivce.VideoMqService;
import com.tanhua.server.serivce.VideoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Map;

@RestController
@Slf4j
public class VideoController {

    @Autowired
    private VideoService videoService;
    @Autowired
    private VideoMqService videoMqService;

    /**
     * 接口名称：小视频列表
     * 接口路径：GET/smallVideos
     */
    @GetMapping("smallVideos")
    public ResponseEntity<Object> queryVideoList(
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer pagesize) {
        // 解决前端页码传入page=0的问题
        if (page < 1) page = 1;
        PageResult pageResult = videoService.queryVideoList(page, pagesize);
        return ResponseEntity.ok(pageResult);
    }

    /**
     * 接口名称：视频上传
     * 接口路径：POST/smallVideos
     */
    @PostMapping("smallVideos")
    public ResponseEntity<Object> uploadVideos(
            MultipartFile videoThumbnail,MultipartFile videoFile,String text) throws IOException {
        log.info("视频上传.....");
        return videoService.uploadVideos(videoThumbnail,videoFile,text);
    }


    /**
     * 接口名称：视频用户关注
     * 接口路径：POST/smallVideos/:uid/userFocus
     */
    @PostMapping("/smallVideos/{uid}/userFocus")
    public ResponseEntity<Object> followUser(@PathVariable("uid") Long followUserId) {
        log.info("接口名称：视频用户关注");
        return videoService.followUser(followUserId);
    }

    /**
     * 接口名称：视频用户关注-取消
     * 接口路径：POST/smallVideos/:uid/userUnFocus
     */
    @PostMapping("/smallVideos/{uid}/userUnFocus")
    public ResponseEntity<Object> unfollowUser(@PathVariable("uid") Long followUserId) {
        log.info("接口名称：视频用户关注-取消");
        return videoService.unfollowUser(followUserId);
    }
    /**
     * 接口名称：视频点赞
     * 接口路径：POST/smallVideos/:id/like
     */
    @PostMapping("smallVideos/{id}/like")
    public ResponseEntity<Object> videosLike(@PathVariable("id") String videoId){
        log.info("接口名称：视频点赞");
        return videoService.videosLike(videoId);
    }

    /**接口名称：视频取消
     * POST/smallVideos/:id/like"
     */
    @PostMapping("smallVideos/{id}/dislike")
    public ResponseEntity<Object> disvideosLike(@PathVariable("id") String videoId){
        log.info("接口名称：视频点赞");
        return videoService.disvideosLike(videoId);
    }

    /**
     * 接口名称：评论列表
     * 接口路径：GET/smallVideos/:id/comments
     */
    @GetMapping("smallVideos/{id}/comments")
    public ResponseEntity<Object> queryVideoCommentList(@PathVariable("id") String videoId,
                                               @RequestParam(defaultValue = "1")Integer page,
                                               @RequestParam(defaultValue = "10")Integer pagesize){
    return videoService.queryVideoCommentList(videoId,page,pagesize);
    }
    /**
     * 接口名称：评论点赞
     * 接口路径：POST/smallVideos/comments/:id/like  videoLike
     */
    @PostMapping("smallVideos/comments/{id}/like")
    public ResponseEntity<Object> videoLike(@PathVariable("id") String commentId){
        log.info("接口名称：视频点赞");
        return videoService.videoCommentLike(commentId);
    }
    /**
     * 接口名称：视频评论发布
     * POST/smallVideos/:id/comment
     */
    @PostMapping("smallVideos/{id}/comments")
    public ResponseEntity<Object> savevideoComment(@PathVariable("id")String videoId,
                                                   @RequestBody Map<String,String>resultMap){
        String comment = resultMap.get("comment");
        return videoService.savevideoComment(videoId,comment);
    }
}
